//
//  AppDelegate.m
//  SuperRadio
//
//  Created by Damon on 14/12/17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import "HomeViewController.h"
#import "AudioPlayer.h"
#import "WeiboSDK.h"
#import "LoginViewController.h"

#define kAppKey  @"2896537712"
#define kRedirectURI @"http://www.sina.com"
@interface AppDelegate ()<UITabBarControllerDelegate,WeiboSDKDelegate>

@property(nonatomic, retain) UINavigationController *navi;



@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 后台播放
    
    
//    AVAudioSession *session = [AVAudioSession sharedInstance];
//    if ([session setCategory:AVAudioSessionCategoryPlayback error:nil]) {
//        if ([session setActive:YES error:nil]) {
//            NSLog(@"后台播放准备完毕");
//        }
//    }
    
    

    

//    AVAudioSession *session = [AVAudioSession sharedInstance];
//    [session setActive:YES error:nil];
//    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:kAppKey];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    
    UITabBarController *tabbarVC = [[UITabBarController alloc] init];
    
    
    // 首页
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    homeVC.title = @"首页";
    homeVC.tabBarItem.image = [UIImage imageNamed:@"gerenzhuye.png"];
    
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:homeVC];
    tabbarVC.delegate = self;
    
    // 登陆页面
    LoginViewController *loginVC =[[LoginViewController alloc] init];
    loginVC.title = @"我的";
    loginVC.tabBarItem.image = [UIImage imageNamed:@"wode.png"];
    
    UINavigationController *naviTwo = [[UINavigationController alloc] initWithRootViewController:loginVC];
    
    NSArray *arr = [NSArray arrayWithObjects:navi, naviTwo, nil];
    
    tabbarVC.viewControllers = arr;
   // NSArray *arr = [NSArray arrayWithObjects:navi, nil];

    self.window.rootViewController = tabbarVC;
    
   // tabbarVC.viewControllers = arr;
    
    [homeVC release];
    [navi release];
    
   


    
    //[tabbarVC.tabBar addSubview:self.roundView];
    
    [tabbarVC release];
    
    
    
    
    // Override point for customization after application launch.
    [NSThread sleepForTimeInterval:2];
    
    
    
    
    
    
    
    return YES;
}

- (void)playStatuUpdate:(BOOL)playState
{
 
    
  
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}


#pragma mark 微博登陆
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
     if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
//        NSString *title = NSLocalizedString(@"认证结果", nil);
//        NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken],  NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
        
        
        self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
        self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
        
        
        // 给通知中心发送一个消息[登录]
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil userInfo:response.userInfo];
        // 收藏 的通知中心
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zuzhang" object:nil userInfo:response.userInfo];
        
//        // 判断我的收藏 的通知中心
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"chenggong" object:@"成功" userInfo:response.userInfo];
        
//        // 判断收藏 的通知中心
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"collect" object:@"收藏成功" userInfo:response.userInfo];
    }
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self ];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
